package com.jh3502.vivo.function.travelpages.approve;

/**
 * @author harisucici
 * @date 2017/6/19
 */

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jh3502.vivo.R;
import com.jh3502.vivo.function.todo.TodoListBean;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class TravelApproveAdapter extends RecyclerView.Adapter<TravelApproveAdapter.ViewHolder> implements View.OnClickListener {


    private Context mContext;
    private List<TodoListBean> dataList;
    private View convertView;
    @Nullable
    private OnRecyclerViewItemClickListener mOnItemClickListener = null;
    private ButtonInterface viewClickSubmit, viewClickCancel, viewClickDelete;

    public TravelApproveAdapter(Context context) {
        this.mContext = context;
    }

    public TravelApproveAdapter(Context context, List<TodoListBean> datas) {
        this.dataList = datas;
        this.mContext = context;
    }


    public void setButtonSetOnclickSubmit(ButtonInterface buttonInterface) {
        this.viewClickSubmit = buttonInterface;
    }

    public void setButtonSetOnclickCancel(ButtonInterface buttonInterface) {
        this.viewClickCancel = buttonInterface;
    }

    public void setButtonSetOnclickDelete(ButtonInterface buttonInterface) {
        this.viewClickDelete = buttonInterface;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        convertView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_travel_approve, viewGroup, false);
        ViewHolder vh = new ViewHolder(convertView);
        convertView.setOnClickListener(this);
        return vh;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, final int position) {
        viewHolder.itemView.setTag(position);
        viewHolder.itemTravelApproveTitle.setText(dataList.get(position).getName());
        viewHolder.itemTravelApproveNum.setText(dataList.get(position).getBusinessNo());
        viewHolder.itemTravelApproveTravel.setText(dataList.get(position).getDestination());
        if (dataList.get(position).getOvertop() == 0) {
            viewHolder.itemTravelApproveOut.setText("否");
            viewHolder.itemTravelApproveOut.setTextColor(Color.parseColor("#FF747474"));
        } else {
            viewHolder.itemTravelApproveOut.setText("是");
            viewHolder.itemTravelApproveOut.setTextColor(Color.parseColor("#FFF36C60"));

        }
        viewHolder.itemTravelApproveDate.setText(dataList.get(position).getCreateDate());
        int state = dataList.get(position).getState();
        if (state == 0) {
            Glide.with(mContext).load(R.drawable.point_red).into(viewHolder.itemTravelApprovePoint);
            viewHolder.itemTravelApproveStatus.setText("待审批");
            viewHolder.itemTravelApproveStatus.setTextColor(Color.parseColor("#FFF36C60"));
            viewHolder.itemTravelApproveSubmit.setVisibility(View.VISIBLE);
            viewHolder.itemTravelApproveCancel.setVisibility(View.VISIBLE);
            viewHolder.itemTravelApproveDelete.setVisibility(View.GONE);
        } else if (state == 1) {
            Glide.with(mContext).load(R.drawable.point_green).into(viewHolder.itemTravelApprovePoint);
            viewHolder.itemTravelApproveStatus.setText("已批准");
            viewHolder.itemTravelApproveStatus.setTextColor(Color.parseColor("#FF0EC053"));
            viewHolder.itemTravelApproveSubmit.setVisibility(View.GONE);
            viewHolder.itemTravelApproveCancel.setVisibility(View.GONE);
            viewHolder.itemTravelApproveDelete.setVisibility(View.VISIBLE);
        } else if (state == 2) {
            Glide.with(mContext).load(R.drawable.point_blue).into(viewHolder.itemTravelApprovePoint);
            viewHolder.itemTravelApproveStatus.setText("审批被拒绝");
            viewHolder.itemTravelApproveStatus.setTextColor(Color.parseColor("#FF3389FF"));
            viewHolder.itemTravelApproveSubmit.setVisibility(View.GONE);
            viewHolder.itemTravelApproveCancel.setVisibility(View.GONE);
            viewHolder.itemTravelApproveDelete.setVisibility(View.VISIBLE);
        }

        viewHolder.itemTravelApproveSubmit.setOnClickListener(arg0 -> {
            // TODO Auto-generated method stub
            if (viewClickSubmit != null) {
                viewClickSubmit.onClick(position);
            }
        });
        viewHolder.itemTravelApproveCancel.setOnClickListener(arg0 -> {
            // TODO Auto-generated method stub
            if (viewClickCancel != null) {
                viewClickCancel.onClick(position);
            }
        });
        viewHolder.itemTravelApproveDelete.setOnClickListener(arg0 -> {
            // TODO Auto-generated method stub
            if (viewClickDelete != null) {
                viewClickDelete.onClick(position);
            }
        });
    }

    @Override
    public void onClick(@NonNull View v) {
        if (mOnItemClickListener != null) {
            mOnItemClickListener.onItemClick((int) v.getTag());
        }
    }


    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    public interface ButtonInterface {
        void onClick(int position);
    }

    public interface OnRecyclerViewItemClickListener {
        void onItemClick(int s);
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.item_travel_approve_point)
        ImageView itemTravelApprovePoint;
        @BindView(R.id.item_travel_approve_date)
        TextView itemTravelApproveDate;
        @BindView(R.id.item_travel_approve_cancel)
        Button itemTravelApproveCancel;
        @BindView(R.id.item_travel_approve_submit)
        Button itemTravelApproveSubmit;
        @BindView(R.id.item_travel_approve_delete)
        Button itemTravelApproveDelete;
        @BindView(R.id.item_travel_approve_title)
        TextView itemTravelApproveTitle;
        @BindView(R.id.item_travel_approve_status)
        TextView itemTravelApproveStatus;
        @BindView(R.id.item_travel_approve_num)
        TextView itemTravelApproveNum;
        @BindView(R.id.item_travel_approve_travel)
        TextView itemTravelApproveTravel;
        @BindView(R.id.item_travel_approve_out)
        TextView itemTravelApproveOut;

        public ViewHolder(@NonNull View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }


}